//
//  SINAOtherLoginPasswordViewController.m
//  PlanTech
//
//  Created by ruifeng song on 2018/7/4.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAOtherLoginPasswordViewController.h"
#import "SINAIdentitySelectViewController.h"
#import "SINALoginViewController.h"
#import "SINAStudentTabBarViewController.h"
#import "SINATeacherTabBarViewController.h"
#import "SINANavLoginViewController.h"
#import "PTQQSdkCall.h"
#import "WXApiManager.h"

@interface SINAOtherLoginPasswordViewController ()
@property (weak, nonatomic) IBOutlet UIButton *otherLoginButton;
@property (weak, nonatomic) IBOutlet UITextField *otherLoginPasswordTF;

@end

@implementation SINAOtherLoginPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title =@"登录";
    
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
    //设置lable的圆角显示
    self.otherLoginButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.otherLoginButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.otherLoginButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.otherLoginButton.clipsToBounds = YES;
    [self.otherLoginButton setBackgroundColor: baseButtonBackGroundColor];
    //self.navigationItem.leftBarButtonItem= backButton;
//    [self.otherLoginButton setTitleColor:baseButtonBackGroundColor forState:UIControlStateNormal];
}

- (IBAction)onClickOtherLoginButton:(id)sender {
    
    [self.view endEditing:YES];
    NSString *userName = self.phoneNum;
    NSString *userPassword = self.otherLoginPasswordTF.text;
    
    if (userName==nil||[userName isEqualToString:@""]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    if (userPassword==nil||[userPassword isEqualToString:@""]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入密码" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:userName forKey:@"userPhone"];
    [userDefaults synchronize];
    
    //登录请求参数
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //1.学生phone
    [dict setObject:userName forKey:@"phone"];
    //    NSString *passwordSign = [SINAMd5Utils md5:userPassword];
    //2.未加密密码
    [dict setObject:userPassword forKey:@"passwords"];
    //获取系统时间戳
    NSDate* date1 = [NSDate date];
    NSTimeInterval time1 =[date1 timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f",time1];
    //3.签名时间 当前时间
    [dict setObject:timeString forKey:@"signtimes"];
    //4.唯一标识符
    NSString *uuid = [NSUUID UUID].UUIDString;
    [dict setObject:uuid forKey:@"signuid"];
    //5.签名
    NSString *signflag = [SINAMd5Utils md5:[NSString stringWithFormat:@"%@%@",uuid,timeString]];
    [dict setObject:signflag forKey:@"signflag"];
    
    if (self.otherLoginTag ==1) {
        
        NSString * openId = [PTQQSdkCall getInstance].oauth.openId;
        
        [dict setObject:openId forKey:@"qqopenid"];
        
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSString * qqnickname = [userDefaults objectForKey:@"qqnickname"];
        
        if (qqnickname!=nil ||qqnickname.length !=0) {
            [dict setObject:qqnickname forKey:@"qqnickname"];
        }
    }
    else if (self.otherLoginTag ==2)
    {
        [dict setObject:[WXApiManager sharedManager].openid forKey:@"weixinopenid"];
        [dict setObject:[WXApiManager sharedManager].nickname forKey:@"weixinnickname"];
    }
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    
    [self startLoading];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userLoginUrl] withParamters:dict2 success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        
        [weakSelf stopLoading];
        
        if ([code isEqualToString:@"1000"]) {
            NSDictionary *result = responseData[@"content"];
            
            SINAUserModel *userModel=[SINAUserModel userWithDict:result];
            SINACommonUserHelper *userHelper=[SINACommonUserHelper share];
            userModel.userPassword =userPassword;
            [userHelper saveUserModel:userModel];
            
            
            if (userModel.userUid==nil||[userModel.userUid isEqualToString:@""]) {
                //登录页面
                SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
                SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
                [self restoreRootViewController:homeController];
                
            } else {
                if (userModel.identitys==nil||[userModel.identitys isEqualToString:@"-1"]) {
                    SINAIdentitySelectViewController *selectControll = [[SINAIdentitySelectViewController alloc]initWithNibName:@"SINAIdentitySelectViewController" bundle:nil];
                    [self.navigationController pushViewController:selectControll animated:YES];
                }else{
                    ////用户身份标识  1学生  2教师 -1没有选择身份
                    //home页面
                    if ([userModel.identitys isEqualToString:@"1"]) {
                        SINAStudentTabBarViewController *tabBarViewController = [[SINAStudentTabBarViewController alloc]init];
                        [self restoreRootViewController:tabBarViewController];
                    } else {
                        SINATeacherTabBarViewController *tabBarViewController = [[SINATeacherTabBarViewController alloc]init];
                        [self restoreRootViewController:tabBarViewController];
                    }
                }
            }
        }else if ([code isEqualToString:@"1003"]) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"手机号或者密码错误" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }
    } failure:^(NSError *error, NSInteger errCode) {
        
        [weakSelf stopLoading];
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"登录验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

- (void)restoreRootViewController:(UIViewController *)rootViewController
{
    typedef void (^Animation)(void);
    UIWindow* window = self.view.window;
    
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        window.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
